package lxbAdmin.userSystem.server.impl;

import com.alibaba.fastjson.JSON;
import io.netty.util.internal.StringUtil;
import lxbAdmin.userSystem.dao.LoginMapper;
import lxbAdmin.userSystem.dto.SystemMenuDTO;
import lxbAdmin.userSystem.dto.User;
import lxbAdmin.userSystem.entity.SystemUser;
import lxbAdmin.userSystem.server.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Decoder;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginMapper loginMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public SystemUser login(User user) {
        try {
        	System.out.println("登陆接口");
            if(StringUtil.isNullOrEmpty(user.getLoginName()) || StringUtil.isNullOrEmpty(user.getPassword()))
                return null;
            //base64解码
            String keyWord = new String(new BASE64Decoder().decodeBuffer(user.getPassword()));
            SystemUser result = loginMapper.login(user.getLoginName(),keyWord);
            redisTemplate.opsForValue().set(result.getUserId(),
                    JSON.toJSONString(result),
                    2,
                    TimeUnit.HOURS);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
